package com.apes.fico.engine.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Map;

/**
 * 功能：业务凭证
 *
 * @author xul
 * @create 2019-01-24 15:33
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BusinessDocument.CONST_TABLE_NAME, indexes = {@Index(columnList = "business_type, business_key, voucher_template_id", unique = true)})
public class BusinessDocument extends BaseModel {
    public static final String CONST_TABLE_NAME = "fin_business_document";

    @Id
    @Column(name = "id", length = 32, nullable = false)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "业务凭证", padding = 8)
    @Comment("编号")
    private String id;

    @ManyToOne
    @JoinColumn(name = "voucher_template_id", referencedColumnName = "id", nullable = false)
    @Comment("凭证模板")
    private VoucherTemplate voucherTemplate;

    @Column(name = "business_type", length = 64, nullable = false)
    @Comment("业务凭证类型")
    private String businessType;

    @Column(name = "business_key", length = 32, nullable = false)
    @Comment("业务凭证编号")
    private String businessId;

    @States(name = "订单状态", values = {
            @State(id = "pending", name = "待生成"),
            @State(id = "unusual", name = "异常的"),
            @State(id = "generated", name = "已生成")})
    @Column(name = "state", length = 10, nullable = false)
    private String state = "pending";

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "post_date")
    @Comment("记账日期")
    private Date postDate;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Comment("处理消息")
    @Column(name = "message", columnDefinition = "CLOB")
    private String message;

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

    @Name
    @Transient
    private Map tempVoucher;

    @Transient
    private boolean err;
}
